<?php
namespace jy_exchange;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class model extends \plugin{
	function create($order,$type=0){
		$this->order=$order;		
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['price'],
			'plugin_name'=>'jy_exchange',
			'commission_money'=>0,
			'area_money'=>0,
			'xuni_money'=>0,
			'jifen'=>0,
			'red_money'=>0,
			'createtime'=>TIMESTAMP,
		);
		//file_put_contents(ROOT_D.'/1.txt',$order['api_data']);
		$api_data=json_decode($order['api_data'],true);
		if(!$api_data['fenfee']){
			return false;
		}
		$member=pdo_get('jy_commission_member',array('mid'=>$order['mid']));
		$agent_id=$member['agent_id'];
		if(!$agent_id){
			return false;
		}
		$agent=pdo_get('jy_commission_member',array('mid'=>$agent_id));
		$this->create_redcommission($agent,$api_data['fenfee']);
		//生成分销订单
		if($this->commission_order['isarea']){
			$indata=$this->commission_order;
			unset($indata['isarea']);
			unset($indata['maxlevel']);
			unset($indata['red_money']);
			pdo_insert('jy_commission_order',$indata);
		}
		return true;
	}
	function create_redcommission($agent,$fenfee){
			if($fenfee>0){
				$this->commission_order['commission_money']+=$fenfee;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>0,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$fenfee,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>24,
				);
				pdo_insert('jy_commission_commission',$new_commission);
				$this->commission_order['isarea']=true;
			}
			return true;
	}
}